{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 145. Binary Tree Postorder Traversal\n",
    "\n",
    "Given the root of a binary tree, return the postorder traversal of its nodes' values.\n",
    "\n",
    "    Example 1:\n",
    "\n",
    "<div align=\"center\">\n",
    "    <img src=\"./images/pre1.jpg\" width=20%>\n",
    "</div>\n",
    "\n",
    "        Input: root = [1,null,2,3]\n",
    "        Output: [3,2,1]\n",
    "\n",
    "    Example 2:\n",
    "        Input: root = []\n",
    "        Output: []\n",
    "\n",
    "    Example 3:\n",
    "        Input: root = [1]\n",
    "        Output: [1]\n",
    "\n",
    "    Example 4:\n",
    "\n",
    "<div align=\"center\">\n",
    "    <img src=\"./images/pre3.jpg\" width=20%>\n",
    "</div>    \n",
    "\n",
    "        Input: root = [1,2]\n",
    "        Output: [2,1]\n",
    "\n",
    "    Example 5:\n",
    "\n",
    "<div align=\"center\">\n",
    "    <img src=\"./images/pre2.jpg\" width=20%>\n",
    "</div>    \n",
    "\n",
    "        Input: root = [1,null,2]\n",
    "        Output: [2,1]\n",
    "\n",
    "Constraints:\n",
    "\n",
    "    - The number of the nodes in the tree is in the range [0, 100].\n",
    "    - -100 <= Node.val <= 100\n",
    " \n",
    "\n",
    "Follow up:\n",
    "\n",
    "Recursive solution is trivial, could you do it iteratively?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from typing import List\n",
    "\n",
    "\n",
    "class TreeNode:\n",
    "    def __init__(self, val=0, left=None, right=None):\n",
    "        self.val = val\n",
    "        self.left = left\n",
    "        self.right = right\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def postorderTraversal(self, root: TreeNode) -> List[int]:\n",
    "        \"\"\"\n",
    "\n",
    "        :param root:\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        def postorder(root: TreeNode):\n",
    "            nonlocal res\n",
    "            if not root:\n",
    "                return\n",
    "            postorder(root.left)\n",
    "            postorder(root.right)\n",
    "            res.append(root.val)\n",
    "\n",
    "        res = []\n",
    "        postorder(root)\n",
    "        return res\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    S = Solution()\n",
    "    root = TreeNode(1)\n",
    "    root.right = TreeNode(2)\n",
    "    root.right.left = TreeNode(3)\n",
    "    print(S.postorderTraversal(root))\n",
    "    print('done')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}